package product;

import models.BaseModel;
import models.constants.DeletedStatus;
import models.weixin.WebUser;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/20.
 */
@Entity
@Table(name="product_need")
public class ProductNeed extends BaseModel {
    @Column(name="show_order")
    public Integer showOrder;

    @JoinColumn(name = "web_user_id", nullable = true)
    @ManyToOne
    public WebUser webUser;

    @Column(name="content")
    public String content;


    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;


    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Product> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.webUser.nickName like {name} ~/")
                .append("/~ and t.content like {content} ~/");

        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Product> orderPage = new JPAExtPaginator<Product>("ProductNeed t", "t", Product.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }



}
